import {Pipe, PipeTransform} from '@angular/core';
import {exists, missing} from '../../../utils/utils';
import {ExpressionService} from "@core/services/expression/ExpressionService";

@Pipe({
  name: 'dataFormat'
})
export class DataFormatPipe implements PipeTransform {

  constructor(protected expressionService: ExpressionService) {

  }

  transform(value: any, args: [any]): any {
    if (missing(value)) return '';
    let result = '';
    for (let expression of args) {
      if (missing(expression)) continue;
      result = this.expressionService.evaluate(expression, {value: value});
      if(exists(result)) return result;
    }
    return value;
  }

}
